import numpy as np

def generate_feature_vector(ndim: int = 10):
    vector = np.random.uniform(0.0, 0.2, ndim)
    indices = np.random.choice(ndim, ndim//3, replace=False)
    for index in indices:
        vector[index] = np.random.uniform(0.6, 1)  # 高偏好权重设置在0.7至1之间
    vector /= np.sum(vector)
    return vector

def cosine_similarity(a: np.ndarray, b: np.ndarray) -> np.ndarray:
    if b.ndim == 1:
        b = b.reshape(1, -1)
    if np.allclose(a, 0) or np.allclose(b, 0):
        return np.dot(a, b.T)
    return np.dot(a, b.T) / (np.linalg.norm(a) * np.linalg.norm(b, axis=1))
